package org.example.utils;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializerFeature;
import org.example.domain.response.ResponseResult;
import org.example.domain.response.ResponseResultEnum;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * response返回的工具类
 */
public class ResponseUtils {

    //返回失败信息
    public static void sendFailedString(HttpServletResponse response, String message) throws IOException {
        response.setContentType("application/json;charset=utf-8");
        //第二个参数让返回的json数据属性为null时也会返回
        String json = JSON.toJSONString(ResponseResult.failed(message), SerializerFeature.WriteMapNullValue);
        response.getWriter().write(json);
    }
    //返回失败代码和信息
    public static void sendFailedString(HttpServletResponse response, String code,String message) throws IOException {
        response.setContentType("application/json;charset=utf-8");
        String json = JSON.toJSONString(ResponseResult.failed(code, message), SerializerFeature.WriteMapNullValue);
        response.getWriter().write(json);
    }
}
